/* 
 * StikiWeb Wiki - a Wiki for Hierarchical Organizations
 * $Id: StikiWebContext.java,v 1.3 2006/07/20 21:14:37 biglee Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the StikiWeb Wiki.
 *
 * The Initial Developer of the Original Code is Lee Haslup (biglee@haslups.com).
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** 
 */
package org.stikiweb.contentMagnager;

import javax.servlet.http.HttpServletRequest;

/**
 * @author biglee
 *
 * StikiWebContext is a singleton class that offers a convenient 
 * place to hang onto application scoped information such as the
 * real path to the wiki page base.
 */
public class StikiWebContext {
	
	// Setting this to true makes the app mutter on Sysout.
	// Please NEVER check in code that sets this to true.
	public final static boolean verbose = false; // NOCHECKIN IF TRUE
	
	private static StikiWebContext solo;
	
	public String wikiBase;
	
	private StikiWebContext() {};
	/**
	 * The preferred way to get an an instance of this class
	 * is to call the solo() method that takes a HttpServletRequest
	 * as a parameter.  This method will initialize the singleton
	 * instance if it does not already exist.  The other method
	 * (that doesn't take a parameter) cannot initialize the 
	 * singleton and will return null.
	 * 
	 * @author biglee
	 *
	 */
	public static StikiWebContext solo(HttpServletRequest req) {
		if(solo==null) {
			solo = new StikiWebContext();
			solo.wikiBase = 
				req.getSession()
				.getServletContext()
				.getRealPath("/WEB-INF/wikiBase/")
				+"/";
		}
		return solo;
	}
	
	/**
	 * This method returns the singleton instance if there is one
	 * but returns null if it is not yet initialized.  This method
	 * should only be called if you are sure that the singleton
	 * instance has been initialized.
	 * @return the singleton instance or null.
	 */
	public static StikiWebContext solo(){
		return solo;
	}
	
	public String debugDump() {
		String retval = PermissionManager.debugDump();
		retval += PageInfoNode.debugDump();
		return retval;
	}
	
	public static void reset() {
		// If the singleton instance exists uninitialize it so the garbage collector can work
		if(solo!=null) {
			solo.wikiBase = null;
			solo = null;
		}
	}
	
	public static void mutter(String s) {
		if(verbose) {
			System.out.println(s);
		}
	}
}
